package com.example.leetcode;

public class Leetcode141 {

    public boolean hasCycle(ListNode head) {
        if(head==null){
            return false;
        }
        ListNode slow=head;
        ListNode fast=head.next;
        while (slow!=fast){
            if(slow==null||fast==null||fast.next==null){
                return false;
            }
            slow=slow.next;
            fast=fast.next.next;
        }

        return true;
    }

    public static void main(String[] args) {

        ListNode head1=new ListNode(1);
        ListNode head2=new ListNode(2);
        head1.next=head2;

//        ListNode head3=new ListNode(3);
//        head2.next=head3;


        boolean b = new Leetcode141().hasCycle(head1);
        System.out.println(b);
    }
}
